package org.hyy.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hyy.dao.ProductDAO;
import org.hyy.dao.UserDAO;
import org.hyy.vo.Product;
import org.hyy.vo.User;

/**
 * 用户操作
 * @author yuanyuan.han
 *
 */
public class UserAction extends BasicAction {
	private static final long serialVersionUID = 8008798782895563576L;
	private String error;	//错误提示
	private String showpages;	//显示页面类型
	private List<Product> newproducts;
	private List<Product> phones;
	private List<Product> computers;
	private List<Product> cameras;
	private List<Product> shoes;
	private User my;
	
	public User getMy() {
		return my;
	}

	public List<Product> getPhones() {
		return phones;
	}

	public List<Product> getComputers() {
		return computers;
	}

	public List<Product> getCameras() {
		return cameras;
	}

	public List<Product> getShoes() {
		return shoes;
	}

	public List<Product> getNewproducts() {
		return newproducts;
	}

	public String getShowpages() {
		return showpages;
	}

	public String getError() {
		return error;
	}

	/**
	 * 首页
	 * @return
	 */
	public String welcomeUser(){
		ProductDAO productDAO = new ProductDAO();
		newproducts = productDAO.getTopTenProducts();
		phones = productDAO.getTopFiveProducts(14);
		computers = productDAO.getTopFiveProducts(15);
		cameras = productDAO.getTopFiveProducts(16);
		shoes = productDAO.getTopFiveProducts(17);
		return "goindex";
	}
	
	/**
	 * 打开登录页面
	 * @return
	 */
	public String gologUser(){
		return "gologin";
	}
	
	/**
	 * 打开注册说明页面
	 * @return
	 */
	public String goregtipUser(){
		return "goregtip";
	}
	
	/**
	 * 打开注册页面
	 * @return
	 */
	public String goregUser(){
		return "goreg";
	}

	/**
	 * 登录用户
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String loginUser(){
		HttpServletRequest request = getRequest();
		String username = request.getParameter("username");
		String password	= request.getParameter("passwd");
		String verifycode = request.getParameter("verifycode");
		String code	= (String)session.get("rand");
		
		if(!code.equals(verifycode)){
			error = "验证码错误";
			return "loginerr";
		}else{
			UserDAO userDAO = new UserDAO();
			boolean flag = userDAO.login(username, password);
			if(flag){
				showpages = "log";
				session.put("user", username);
			}else{
				return "loginerr";
			}
		}
		return "loginsuccess";
	}
	
	/**
	 * 退出登录
	 * @return
	 */
	public String logoutUser(){
		session.remove("user");
		return "logout";
	}
	
	/**
	 * 注册用户
	 * @return
	 */
	public String regUser(){
		HttpServletRequest request = getRequest();
		//取参数
		String username = request.getParameter("username");
		String password = request.getParameter("passwd");
		String phone = request.getParameter("phone");
		String addr = request.getParameter("addr");
		//封装到类
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setPhone(phone);
		user.setAddr(addr);
		user.setRdate(new Date());
		//添加记录
		UserDAO userDAO = new UserDAO();
		boolean result = userDAO.insertOne(user);

		//参数返回
		if(!result){
			//成功
			showpages="reg";
			return "regsuccess";
		}
		else{
			//失败
			return "regfailed";
		}
	}
	
	/**
	 * 用户中心
	 * @return
	 */
	public String homeUser(){
		return "userhome";
	}
	
	/**
	 * 用户中心删除用户
	 * @return
	 */
	public String deleteUser(){
		HttpServletRequest request = getRequest();
		//取参数
		String uid = request.getParameter("uid");
		UserDAO userDAO = new UserDAO();
		userDAO.delete(Integer.valueOf(uid));
		return "delete";
	}
	
	public String infoUser(){
		UserDAO userDAO = new UserDAO();
		my = userDAO.getUserinfoByName((String)session.get("user"));
		return "myinfo";
	}
	
	public String changeUser(){
		HttpServletRequest request = getRequest();
		//取参数
		String id = request.getParameter("id");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String phone = request.getParameter("phone");
		String addr = request.getParameter("addr");
		User user = new User();
		user.setId(Integer.valueOf(id));
		user.setUsername(username);
		user.setPassword(password);
		user.setPhone(phone);
		user.setAddr(addr);
		UserDAO userDAO = new UserDAO();
		userDAO.changeUserById(user);
		return "changeuser";
	}
}
